package cn.wolfcode.controller;

import cn.wolfcode.domain.Destination;
import cn.wolfcode.domain.Strategy;
import cn.wolfcode.domain.Travel;
import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.search.domain.DestinationEs;
import cn.wolfcode.search.domain.StrategyEs;
import cn.wolfcode.search.domain.TravelEs;
import cn.wolfcode.search.domain.UserInfoEs;
import cn.wolfcode.search.query.SearchQueryObject;
import cn.wolfcode.search.service.ISearchService;
import cn.wolfcode.search.vo.SearchResultVO;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.IStrategyService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserInfoService;
import cn.wolfcode.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SearchController {

    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private ISearchService searchService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ITravelService travelService;
    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws UnsupportedEncodingException {
        //设置编码
        qo.setKeyword(URLDecoder.decode(qo.getKeyword(),"UTF-8"));
        //做判断,每种类型不一样 返回值就不一样
        switch (qo.getType()){
            //返回查询目的地的内容
            case SearchQueryObject.TYPE_DEST:
                return searchDest(qo);
                //返回攻略
            case SearchQueryObject.TYPE_STRATEGY:
                return searchStrategy(qo);
                //返回游记
            case SearchQueryObject.TYPE_TRAVEL:
                return searchTravel(qo);
                //返回用户
            case SearchQueryObject.TYPE_USER:
                return searchUserInfo(qo);
            default:
                //查全文 只要有关键字,全部返回
                return searchAll(qo);
        }
    }

    private Object searchAll(SearchQueryObject qo) {
        //得到vo对象
        SearchResultVO vo = new SearchResultVO();
        //初始化所有内容加一块的长度
        long total = 0L;
        //高亮 全文查找 用户
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfo.class, qo, "city", "intro");
        vo.setUsers(us.getContent());
        total += us.getTotalElements();
        //高亮 游记查询
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                Travel.class, qo, "title", "summary");
        //getContent() 拿到全部的数据集合
        vo.setTravels(ts.getContent());
        //getTotalElements() 拿到总数据页的数据长度
        total += ts.getTotalElements();
        //攻略 全文查询 高亮
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                Strategy.class, qo, "title", "summary","subtitle");
        vo.setStrategys(sts.getContent());
        total += sts.getTotalElements();
        //目的地 高亮 全文搜索
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME,
                Destination.class, qo, "info", "name");
        vo.setDests(ds.getContent());
        total += ds.getTotalElements();
        vo.setTotal(total);
        HashMap<String, Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    private Object searchUserInfo(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME,
                UserInfo.class, qo, "city", "intro");
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,
                Travel.class, qo, "title", "summary");
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME,
                Strategy.class, qo, "title", "summary","subtitle");
        HashMap<String, Object> map = new HashMap<>();
        map.put("page",page);
        map.put("qo",qo);
        return JsonResult.success(map);
    }

    private Object searchDest(SearchQueryObject qo) {
        //查询输入的 keyword 可以找到目的地
        Destination dest = destinationService.querybyDestName(qo.getKeyword());
        //如果是 查询改目的地下的所有攻略 游记 用户
        SearchResultVO vo = new SearchResultVO();
        if (dest!=null){
            //攻略
            List<Strategy> sts = strategyService.queryByDestName(qo.getKeyword());
            vo.setStrategys(sts);
            //游记
            List<Travel> ts = travelService.queryByDestName(qo.getKeyword());
            vo.setTravels(ts);
            //用户
            List<UserInfo> us = userInfoService.queryCity(qo.getKeyword());
            vo.setUsers(us);
            //总条数
            long total = sts.size()+ts.size()+us.size();
            vo.setTotal(total);
        }
        //页面需要 qo dest
        Map<String,Object> map = new HashMap<>();
        map.put("result",vo);
        map.put("qo",qo);
        map.put("dest",dest);
        return JsonResult.success(map);
    }
}
